route.test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* @vitest-environment node */
  2. import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
  3. import fs from "node:fs/promises";
  4. import os from "node:os";
  5. import path from "node:path";
  6. vi.mock("@/lib/auth/session", () => ({
  7. getSession: vi.fn(),
  8. }));
  9. import { getSession } from "@/lib/auth/session";
  10. import { GET, dynamic } from "./route.js";
  11. describe("GET /api/branches/[branch]/[year]/[month]/days", () => {
  12. let tmpRoot;
  13. const originalNasRoot = process.env.NAS_ROOT_PATH;
  14. beforeEach(async () => {
  15. vi.clearAllMocks();
  16. tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), "api-days-"));
  17. process.env.NAS_ROOT_PATH = tmpRoot;
  18. await fs.mkdir(path.join(tmpRoot, "NL01", "2024", "10", "23"), {
  19. recursive: true,
  20. });
  21. });
  22. afterEach(async () => {
  23. process.env.NAS_ROOT_PATH = originalNasRoot;
  24. if (tmpRoot) await fs.rm(tmpRoot, { recursive: true, force: true });
  25. });
  26. it('exports dynamic="force-dynamic" (RHL-006)', () => {
  27. expect(dynamic).toBe("force-dynamic");
  28. });
  29. it("returns 401 when unauthenticated", async () => {
  30. getSession.mockResolvedValue(null);
  31. const res = await GET(
  32. new Request("http://localhost/api/branches/NL01/2024/10/days"),
  33. { params: Promise.resolve({ branch: "NL01", year: "2024", month: "10" }) }
  34. );
  35. expect(res.status).toBe(401);
  36. expect(await res.json()).toEqual({
  37. error: { message: "Unauthorized", code: "AUTH_UNAUTHENTICATED" },
  38. });
  39. });
  40. it("returns 403 when branch user accesses a different branch", async () => {
  41. getSession.mockResolvedValue({
  42. role: "branch",
  43. branchId: "NL01",
  44. userId: "u1",
  45. });
  46. const res = await GET(
  47. new Request("http://localhost/api/branches/NL02/2024/10/days"),
  48. { params: Promise.resolve({ branch: "NL02", year: "2024", month: "10" }) }
  49. );
  50. expect(res.status).toBe(403);
  51. expect(await res.json()).toEqual({
  52. error: { message: "Forbidden", code: "AUTH_FORBIDDEN_BRANCH" },
  53. });
  54. });
  55. it("returns days for a valid branch/year/month when allowed", async () => {
  56. getSession.mockResolvedValue({
  57. role: "admin",
  58. branchId: null,
  59. userId: "u2",
  60. });
  61. const res = await GET(
  62. new Request("http://localhost/api/branches/NL01/2024/10/days"),
  63. { params: Promise.resolve({ branch: "NL01", year: "2024", month: "10" }) }
  64. );
  65. expect(res.status).toBe(200);
  66. expect(await res.json()).toEqual({
  67. branch: "NL01",
  68. year: "2024",
  69. month: "10",
  70. days: ["23"],
  71. });
  72. });
  73. it("returns 400 when any param is missing (authenticated)", async () => {
  74. getSession.mockResolvedValue({
  75. role: "admin",
  76. branchId: null,
  77. userId: "u2",
  78. });
  79. const res = await GET(
  80. new Request("http://localhost/api/branches/NL01/2024//days"),
  81. {
  82. params: Promise.resolve({
  83. branch: "NL01",
  84. year: "2024",
  85. month: undefined,
  86. }),
  87. }
  88. );
  89. expect(res.status).toBe(400);
  90. expect(await res.json()).toEqual({
  91. error: {
  92. message: "Missing required route parameter(s)",
  93. code: "VALIDATION_MISSING_PARAM",
  94. details: { params: ["month"] },
  95. },
  96. });
  97. });
  98. it("returns 404 when the month folder does not exist (authorized)", async () => {
  99. getSession.mockResolvedValue({
  100. role: "admin",
  101. branchId: null,
  102. userId: "u2",
  103. });
  104. const res = await GET(
  105. new Request("http://localhost/api/branches/NL01/2024/99/days"),
  106. { params: Promise.resolve({ branch: "NL01", year: "2024", month: "99" }) }
  107. );
  108. expect(res.status).toBe(404);
  109. expect(await res.json()).toEqual({
  110. error: {
  111. message: "Not found",
  112. code: "FS_NOT_FOUND",
  113. details: { branch: "NL01", year: "2024", month: "99" },
  114. },
  115. });
  116. });
  117. });